package com.etl.user.controller;


import com.etl.base.BaseController;
import com.etl.user.model.User;
import com.etl.user.service.IUserService;
import com.xr.annotation.RemoteService;
import com.xr.dto.ResultDto;
import com.xr.util.Assert;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author Arnold Yang
 * @since 2018-07-30
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

  @RemoteService
  private IUserService userService;

  /**
   * 用户注册 第一步，获取短信验证码
   * @param mobileNumber 手机号
   * @param vCode 验证码
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping("/signup/step/1")
  public ResultDto signupStep1(String mobileNumber, String vCode) throws Exception {
    Assert.hasLength(vCode, "验证码不正确");
    Assert.hasLength(mobileNumber, "手机号不正确");
    Assert.isNumber(mobileNumber, "手机号不合法");

    // TODO 发送短信

    return super.successMessage("短信发送成功");
  }

  /**
   * 用户注册 第二步，验证获取短信验证码 && 注册
   * @param smsCode 第一步获取的短信验证码
   * @param mobileNumber 手机号
   * @param password 加密后的用户密码
   * @return
   * @throws Exception
   */
  @ResponseBody
  @RequestMapping("/signup/step/2")
  public ResultDto signupStep2(String smsCode, String mobileNumber, String password) throws Exception {
    Assert.hasLength(smsCode, "短信验证码不能为空");
    Assert.hasLength(mobileNumber, "手机号不正确");
    Assert.isNumber(mobileNumber, "手机号不合法");
    Assert.hasLength(password, "密码不能为空");

    User newUser = new User();
    newUser.setMobileNumber(mobileNumber);
    newUser.setPassword(password);
    userService.signup(newUser);

    return super.successMessage("短信发送成功");
  }

}

